-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
UPDATED rat_in_maze.py #9148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UPDATED rat_in_maze.py #9148
Conversation
…mmit tests while raising PR.
backtracking/rat_in_maze.py
Outdated
source_column: int, | ||
destination_row: int, | ||
destination_column: int, | ||
) -> list[list[int]] | None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning two different datatypes pushes work on the caller. Please consider raising an exception if there is no solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my remark in the general discussion of this Pull Request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
) -> list[list[int]] | None: | |
) -> list[list[int]]: |
@cclauss Is there something I'm missing? |
Ok so @Muhammadummerr old pull request was reopened here.
@cclaus If an exception is raised, the caller still has to wrap each function call in a try-except clause and catch the correct exception. In Nim, I would use an Option type (the equivalent of Result type in Rust, I guess?). |
It is very true that Nim and Rust have cooler ways of dealing with exception states that Python does but raising Exceptions are quite Pythonic and are preferred to multiple different datatypes. |
Okay,Based on my understanding of your request, the program should return both the solution maze and a flag. |
No. The function should return a solution if there is one and raise an ValueError if there is no solution. |
I have made these changes. |
Okay, my mistake. I need to make some more changes. Thanks. |
Do we really care about changing the initial and destination cells in the maze? I do not really see the benefit of it and it just makes the implementation harder to read and understand. |
backtracking/rat_in_maze.py
Outdated
[0, 0, 0, 0, 1] | ||
True | ||
>>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) | ||
[[0, 1, 1, 1, 1], [0, 0, 0, 0, 1], [1, 1, 1, 0, 1],\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the output on the same line now? It makes reading the solution harder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.python.org/3/library/doctest.html#doctest.NORMALIZE_WHITESPACE will allow you to have doctests that ignore the whitespace so the matrix can look like a matrix.
for more information, see https://pre-commit.ci
All Test case are passed on my device but failing in build check. |
Looking better! |
________________ [doctest] backtracking.rat_in_maze.solve_maze _________________ /home/runner/work/Python/Python/backtracking/rat_in_maze.py:37: DocTestFailure |
This same test case passed on my local device because I used (optionflags=doctest.NORMALIZE_WHITESPACE), so why is it failing here? |
I guess; it would be more helpful for understanding to run the program with different source and destination points to find the path. |
Does this program require any further changes, or is it okay as it is? |
return False | ||
return False | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work?!?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work?!?
Yes, it's working on my local machine, but it's not working on the build test. That's why I added '# doctest: +NORMALIZE_WHITESPACE' to each test.
* UPDATED rat_in_maze.py * Update reddit.py in Webprogramming b/c it was causing error in pre-commit tests while raising PR. * UPDATED rat_in_maze.py * fixed return type to only maze,otherwise raise valueError. * fixed whitespaces error,improved matrix visual. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updated. * Try * updated * updated * Apply suggestions from code review --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
Describe your change:
Fixes #9066
I have made updates to the rat_in_maze.py file to enhance code clarity.
These updates include:
Checklist: